﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SemenQuest.SubWindows
{
    public partial class ColorButton : UserControl
    {

        [Category("Appearance")]
        [DefaultValue(typeof(Color), "Control")]
        [Description("Setting Default Color")]
        public Color DefaultColor
        {
            get { return btn_Default.BackColor; }
            set
            {
                btn_Default.BackColor = value;
                btn_Default.Enabled = value != CurrentColor;
            }
        }
        [Category("Appearance")]
        [DefaultValue(typeof(Color), "Control")]
        [Description("Setting Current Color")]
        public Color CurrentColor
        {
            get { return Btn_Main.BackColor; }
            set
            {
                if (value != Btn_Main.BackColor)
                {
                    Btn_Main.BackColor = value;
                    btn_Default.Enabled = value != DefaultColor;

                    var handler = ColorChanged;
                    if (handler != null)
                        handler(SettingName, value);
                }
                
            }
        }

        [Category("Appearance")]
        [Description("Friendly Title of the control")]
        public String FriendlyName { get { return Btn_Main.Text; } set { Btn_Main.Text = value; } }

        [Category("Behaviour")]
        [Description("Databound setting Name")]
        public String SettingName { get; set; }

        public delegate void ColorChangedHandler(String setting, Color color);
        public event ColorChangedHandler ColorChanged;

        public ColorButton()
        {
            InitializeComponent();
        }

        private void Btn_Main_Click(Object sender, EventArgs e)
        {
            ColorDialog CD = new ColorDialog();
            CD.Color = CurrentColor;
            CD.CustomColors = new Int32[1] { CurrentColor.B << 16 | CurrentColor.G << 8 | CurrentColor.R };
            CD.AllowFullOpen = true;
            CD.FullOpen = false;
            if (CD.ShowDialog(this) == DialogResult.OK)
            {
                CurrentColor = CD.Color;
            }
        }
        private void btn_Default_Click(Object sender, EventArgs e)
        {
            CurrentColor = DefaultColor;
        }
    }
}
